//
//  ViewController.m
//  UIPickerView
//
//  Created by 王恕 on 16/1/26.
//  Copyright © 2016年 王恕. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *myPickerView;
@property (nonatomic, strong) NSArray *province;//存放要选择的数据
@property (nonatomic, strong) UIButton *Btn;//选择按钮
@property (nonatomic, strong) UILabel *provinceLabel;//显示标签
@property (nonatomic, strong) UIView *myView;//确定按钮以及选择器的父视图
@property (nonatomic, strong) NSString *Str;//存放选定以后的数据

@end

@implementation ViewController

- (UIButton *)Btn{

    if (!_Btn) {
        _Btn = [[UIButton alloc] initWithFrame:CGRectMake(275, 242, 46, 30)];
        [_Btn setTitle:@"省份" forState:UIControlStateNormal];
        _Btn.backgroundColor = [UIColor redColor];
        [_Btn addTarget:self action:@selector(myProvince) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_Btn];
    }
    return _Btn;
}

- (UILabel *)provinceLabel{

    if (!_provinceLabel) {
        _provinceLabel = [[UILabel alloc] initWithFrame:CGRectMake(63, 243, 137, 30)];
        _provinceLabel.backgroundColor = [UIColor grayColor];
   
        [self.view addSubview:_provinceLabel];
    }
    return _provinceLabel;
}

//使用懒加载方式加载myPickerView
- (UIPickerView *)myPickerView{

    if (!_myPickerView) {
        
        _myView = [[UIView alloc] initWithFrame:CGRectMake(0, 400, 375, 267)];
        _myView.backgroundColor = [UIColor clearColor];
    
        _myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 375, 216)];
        _myPickerView.dataSource = self;
        _myPickerView.delegate = self;
        [_myView addSubview:_myPickerView];
        
        UIButton *myBtn = [[UIButton alloc] initWithFrame:CGRectMake(329, 229, 50, 30)];
        [myBtn setTitle:@"确定" forState:UIControlStateNormal];
        myBtn.backgroundColor = [UIColor blackColor];
        [myBtn addTarget:self action:@selector(certain) forControlEvents:UIControlEventTouchUpInside];
        [_myView addSubview:myBtn];
        
    }
    return _myPickerView;
}

- (void) myProvince{
   [self.view addSubview:_myView];
}

- (void) certain{
    _provinceLabel.text = _Str;
    [_myView removeFromSuperview];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 1;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    return _province.count;
}

//返回指定列列表项的文本标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    return _province[row];
}

//返回选中列表项调用此方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    _Str = _province[row];
 
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _province = @[@"山西",@"江苏",@"北京"];
    _Str = [[NSString alloc] init];
    [self myPickerView];
    [self provinceLabel];
    [self Btn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
